package com.hbpa.pan.server.modules.file.controller;

import com.google.common.base.Splitter;
import com.hbpa.pan.core.constants.RPanConstants;
import com.hbpa.pan.core.response.ResponseEntity;
import com.hbpa.pan.core.utils.IdUtil;
import com.hbpa.pan.server.modules.file.constants.FileConstants;
import com.hbpa.pan.server.modules.file.context.CreateFolderContext;
import com.hbpa.pan.server.modules.file.context.DeleteFileContext;
import com.hbpa.pan.server.modules.file.context.QueryFilelistContext;
import com.hbpa.pan.server.modules.file.context.UpdateFilenameContext;
import com.hbpa.pan.server.modules.file.converter.FileConverter;
import com.hbpa.pan.server.modules.file.enums.DelFlagEnum;
import com.hbpa.pan.server.modules.file.po.CreateFolderPO;
import com.hbpa.pan.server.modules.file.po.DeleteFilePO;
import com.hbpa.pan.server.modules.file.po.UpdateFilenamePO;
import com.hbpa.pan.server.modules.file.service.IUserFileService;
import com.hbpa.pan.server.modules.file.vo.RPanUserFileVO;
import com.hbpa.pan.server.modules.user.utils.UserIdUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.print.attribute.standard.Media;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: BlcakTiger
 * @date: 2023/10/26 8:31
 * @contact: hsf6661@163.com
 * @description: 文件模块控制器
 */
@RestController
@Validated
@Api(tags = "文件模块")
public class FileController {

    @Autowired
    private IUserFileService iUserFileService;

    @Autowired
    private FileConverter fileConverter;

    @ApiOperation(
            value = "查询文件列表",
            notes = "该接口提供了用户查询某文件夹下面某些文件类型的文件列表的功能",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @GetMapping("/list")
    public ResponseEntity<List<RPanUserFileVO>> list(@NotBlank(message = "父文件夹id不能为空") @RequestParam(value = "parentId",required = false) String parentId,
                                                     @RequestParam(value = "fileType",required = false,defaultValue = FileConstants.ALL_FILE_TYPE)String fileType){
        Long realParentId = IdUtil.decrypt(parentId);
        List<Integer> fileTypeArray = null;
        //判断是否拥有全部文件类型0，若不是，用分隔符进行分割并获取每一个文件类型
        if (!Objects.equals(FileConstants.ALL_FILE_TYPE,fileType)){
            fileTypeArray = Splitter.on(RPanConstants.COMMON_SEPARATOR)
                    .splitToList(fileType)
                    .stream()
                    .map(Integer::valueOf)
                    .collect(Collectors.toList());
        }
        QueryFilelistContext context = new QueryFilelistContext();
        context.setParentId(realParentId);
        //从当前线程获取用户id
        context.setUserId(UserIdUtil.get());
        context.setFileTypeArray(fileTypeArray);
        context.setDelFlag(DelFlagEnum.NO.getCode());

        List<RPanUserFileVO> result = iUserFileService.getFileList(context);
        return ResponseEntity.data(result);
    }

    @ApiOperation(
            value = "创建文件夹",
            notes = "该接口提供了创建文件夹的功能",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/file")
    public ResponseEntity<String> createFolder(@Validated @RequestBody CreateFolderPO createFolderPO){
        CreateFolderContext context = fileConverter.createFolderPO2CreateFolderContext(createFolderPO);
        Long fileId = iUserFileService.createFolder(context);
        //返回加密的文件id
        return ResponseEntity.data(IdUtil.encrypt(fileId));
    }

    @ApiOperation(
            value = "更新文件名称",
            notes = "该接口提供了更新文件名称的功能",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PutMapping("/file")
    public ResponseEntity<String> updateFilename(@Validated @RequestBody UpdateFilenamePO updateFilenamePO){
        UpdateFilenameContext context = fileConverter.updateFilenamePO2UpdateFilenameContext(updateFilenamePO);
        iUserFileService.updateFilename(context);
        //返回加密的文件id
        return ResponseEntity.success();
    }

    @ApiOperation(
            value = "批量删除文件",
            notes = "该接口提供了批量删除文件的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @DeleteMapping("/file")
    public ResponseEntity<String> deleteFile(@Validated @RequestBody DeleteFilePO deleteFilePO){
        DeleteFileContext context = fileConverter.deleteFilePO2DeleteFileContext(deleteFilePO);
        //对文件id进行解密
        String fileIds = deleteFilePO.getFileIds();
        List<Long> fileIdList = Splitter.on(RPanConstants.COMMON_SEPARATOR).splitToList(fileIds).stream().map(IdUtil::decrypt).collect(Collectors.toList());
        //将解密后的文件id封装到上下文对象中以供下游业务使用
        context.setFileIdList(fileIdList);
        iUserFileService.deleteFile(context);
        //返回加密的文件id
        return ResponseEntity.success();
    }
}

